<script>
	export default {
		onLaunch: function() {
			var userInfo = uni.getStorageSync('userinfo');
			// return false;
			// if (userInfo) {
			// 	this.$store.state.$userInfo = userInfo;
			// 	return false;
			// };
			// 非静默授权，第一次有弹框
			let local = window.location.href; // 获取页面url
			console.log('local', local)
			let appid = 'wx106dff8b8fe84fb9' // 公众号appid
			let code = this.getUrlCode().code; // 截取code
			// 获取之前的code
			let oldCode = uni.getStorageSync('wechatCode')
			if (code == null || code === '' || code == 'undefined' || code == oldCode) {
				// 如果没有code，就去请求获取code
				console.log('当前没有code，进入授权页面')
				let uri = encodeURIComponent(local)
				// 设置旧的code为0，避免死循环
				uni.setStorageSync('wechatCode', 0)
				window.location.href =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`

			} else {
				console.log('11111');
				uni.setStorageSync('wechatCode', code)
				uni.request({
					url: 'http://chatadmin.baimajingxuan.com/api/login/loginRes',
					data: { //参数
						code: code
					},
					method: 'POST', //请求方式，必须为大写
					success: (res) => {
						console.log('data', res.data.data);
						this.$store.state.$userInfo = res.data.data;
						uni.setStorageSync('token', res.data.data.token);
						uni.setStorageSync('userinfo', res.data.data);

						uni.request({
							url: 'http://chatadmin.baimajingxuan.com/api/user/index',
							header: { //参数
								token: res.data.data.token
							},
							method: 'POST', //请求方式，必须为大写
							success: (res) => {
								this.$store.state.$userInfo.user_find_num = res.user_find_num
							}
						})
						// getUserInfo().then(res => {
						// 	console.log(res);
						// 	this.$store.state.$userInfo.user_find_num = res.user_find_num
						// })

					}
				});

				// this.$http.post('api/login', {
				// 	'platform': 'wechat',
				// 	'code': code
				// }).then(res => {
				// 	if (res.data.code == 1) {
				// 		uni.setStorageSync('token', res.data.data.userinfo.token)
				// 		uni.setStorageSync('userinfo', res.data.data.userinfo)
				// 		uni.switchTab({
				// 			url: "/pages/index/index"
				// 		})
				// 	} else {
				// 		//重新拉起登录
				// 		let uri = encodeURIComponent(local)
				// 		uni.setStorageSync('wechatCode', 0)
				// 		window.location.href =
				// 			`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
				// 	}
				// }).catch(err => {

				// })
			}
		},
		onShow: function() {

		},
		onHide: function() {

		},
		methods: {
			getUrlCode() {
				// 截取url中的code方法
				var url = location.search;
				// this.winUrl = url;
				var theRequest = new Object();
				if (url.indexOf('?') != -1) {
					var str = url.substr(1);
					var strs = str.split('&');
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
					}
				}
				return theRequest;
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/uview-ui/index.scss";
	@import "common/demo.scss";
</style>
